//
//  DetailViewControl.m
//  NoteDemo
//
//  Created by 郑火 on 16/2/17.
//  Copyright © 2016年 zwz. All rights reserved.
//

#import "DetailViewControl.h"


@interface DetailViewControl ()<NoteBusinessLogicDelegate>

@end
@implementation DetailViewControl

#pragma mark - Managing the detail item
- (IBAction)onClickSave:(id)sender {
    
    Note *note = self.detailItem;
    
    NSDate *date = [[NSDate alloc] init];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy-MM-dd";
    NSString* strDate  = [dateFormatter stringFromDate:date];
    
    note.date = strDate;
    note.content = self.txtView.text;
    [self.nbl modifyNote:note];
    [self.txtView resignFirstResponder];
}

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;
        [self configureView];
    }
}

- (void)configureView
{
    if (self.detailItem) {
        Note *note = _detailItem;
        self.txtView.text = note.content;
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureView];
    
    self.nbl = [NoteBuinessLogic new];
    self.nbl.delegate = self;
    [self.txtView becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}




//修改Note方法 成功
- (void)modifyNoteFinished
{
    
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作信息"
//                                                        message:@"修改成功。"
//                                                       delegate:self
//                                              cancelButtonTitle:@"OK"
//                                              otherButtonTitles: nil];
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:@"修改成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self.navigationController popToRootViewControllerAnimated:YES]; 
        //[self.navigationController popViewControllerAnimated:YES];
//        [self.presentingViewController  dismissViewControllerAnimated:YES completion:nil];
    }];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];
    
}

//修改Note方法 失败
- (void)modifyNoteFailed:(NSError *)error
{
    
    NSString *errorStr = [error localizedDescription];
//    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"操作信息"
//                                                        message:errorStr
//                                                       delegate:self
//                                              cancelButtonTitle:@"OK"
//                                              otherButtonTitles: nil];
    UIAlertController *uac = [UIAlertController alertControllerWithTitle:@"操作信息" message:errorStr preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    [uac addAction:okAction];
    [self presentViewController:uac animated:YES completion:nil];
   }



@end
